package com.activeMQ.student.transcation;

import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;

/**
 * 队列的消息生产者
 * @date 2020/10/7 14:12
 */
public class TestProducer {

//    服务端的ip和端口号
    private static String brokerUrl = "tcp://106.12.51.117:61617";

//    消息队列的名称
    private static String queueName = "First-Message";

    public static void main(String[] args) throws JMSException {
        ConnectionFactory factory = new ActiveMQConnectionFactory(brokerUrl);
        Connection connection = factory.createConnection();
        connection.start();
        Session session = connection.createSession(false,Session.AUTO_ACKNOWLEDGE);
        Queue queue = session.createQueue(queueName);
        MessageProducer producer = session.createProducer(queue);
        TextMessage textMessage = session.createTextMessage();
        for (int i = 0; i < 10; i++) {
            textMessage.setText("Hello ActiveMQ"+i);
            producer.send(textMessage);
        }
        producer.close();
        session.close();
        connection.close();

        System.out.println("消息发送成功");
    }

}
